package edu.zut.cs.software.cms.module.all.clazz.service;


import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import edu.zut.cs.software.cms.base.service.GenericGenerator;
import edu.zut.cs.software.cms.module.all.clazz.domain.Grade;



/**
 * Generator for Grade
 * @author 42276
 *
 */
public class GradeGenerator extends GenericGenerator {

	private static final Logger logger = LogManager.getLogger(GradeGenerator.class.getName());

	List<Grade> gradeList;
	
	GradeManager gradeManager;

	@Autowired
	public void setGradeManager(GradeManager gradeManager) {
		this.gradeManager=gradeManager;
	}
	@Before
	public void setUp() throws Exception {
		this.gradeList = new ArrayList<Grade>();
		InputStream input = GradeManagerTest.class.getResourceAsStream("/javaee_grade_software.xlsx");
		@SuppressWarnings("resource")
		XSSFWorkbook wb = new XSSFWorkbook(input);
		XSSFSheet sheet = wb.getSheetAt(0);
		DataFormatter dataFormatter = new DataFormatter();
		// int rows =sh.getLastRowNum()+1-sh.getFirstRowNum();
		for (int i = 1; i <= sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i);
			Grade g = new Grade();

			for (int j = 0; j < row.getLastCellNum(); j++) {
				Cell cell = row.getCell(j);
				if (cell != null) {
					String value = dataFormatter.formatCellValue(cell);
					if (j == 1)
						g.setDepartment(value);
					if (j == 2)
						g.setGrade_teacher(value);
					if (j == 3)
						g.setMajor(value);
				}
			}
			this.gradeList.add(g);
		}
	}

	@Test
	public void test() {
		//this.gradeManager.deleteAll();
		this.gradeManager.save(this.gradeList);
		Iterable<Grade> result = this.gradeManager.findAll();
		if (logger.isInfoEnabled()) {
			logger.info("test() - List<Grade> result=" + result); //$NON-NLS-1$
		}

	}
}


